﻿using BAL;
using DAL.DataTransferObject;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLBH
{
    public partial class frmLogin : Form
    {
        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int LPAR);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        const int WM_NCLBUTTONDOWN = 0xA1;
        const int HT_CAPTION = 0x2;

        private void move_window(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }
        public frmLogin()
        {
            this.MouseDown += new MouseEventHandler(move_window);
            InitializeComponent();
            this.AcceptButton = btnLogin;
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {

        }

        private void button1_MouseHover(object sender, EventArgs e)
        {
            btnClose.BackColor = Color.Crimson;
        }

        private void button1_MouseLeave(object sender, EventArgs e)
        {
            btnClose.BackColor = Color.Transparent;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        NhanVienBAO nvBAO = new NhanVienBAO();
        private void button3_Click(object sender, EventArgs e)
        {
            bool LoginFailed = true;
            List<NhanVien> list = nvBAO.GetListNhanVien();
            foreach (NhanVien item in list)
                if (item._account == txtID.Text && item._password == txtPassword.Text)
                {
                    LoginFailed = false;
                    NhanVien obj = new NhanVien();
                    obj.tenNV = item.tenNV;
                    obj.chucVu = item.chucVu;
                    obj._level = item._level;
                    obj._account = item._account;
                    frmMain frm = new frmMain(obj);
                    frm.ShowDialog();
                    if (frmMain.isQuit)
                        this.Close();
                    txtID.Text = txtPassword.Text = "";
                }
            if (LoginFailed)
            {
                MessageBox.Show("Tên đăng nhập hoặc mật khẩu sai ! Mời đăng nhập lại !");
                txtID.Focus();
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnMini_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void frmLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnLogin.PerformClick();
            if (e.KeyCode == Keys.Escape)
                this.WindowState = FormWindowState.Minimized;
        }

        private void FormDragDrop(object sender, EventArgs e)
        {
            var tmp = sender as Control;
            move_window(tmp.Parent, (MouseEventArgs)e);
        }

        private void linkChangePW_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmDoiPass frm = new frmDoiPass();
            frm.ShowDialog();
        }


    }
}
